/**
 * Copyright 2010 Balaji Thennarangam.
 */
package com.kathez.studentportal.business;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import com.kathez.CRUDOperataionType;
import com.kathez.KathezObjectFactory;
import com.kathez.business.AbstractKathezDelegatableBusiness;
import com.kathez.dao.DAOInsertResult;
import com.kathez.dao.IKathezDao;
import com.kathez.entity.BusinessOperable;
import com.kathez.studentportal.entity.Student;

/**
 * @author Balaji T
 *
 */
public class EnrollmentImpl extends AbstractKathezDelegatableBusiness implements Enrollment {

	/**
	 * 
	 */
	public EnrollmentImpl() {
		super();
	}
	
	/* (non-Javadoc)
	 * @see com.kathez.business.IKathezDelegatableBusiness#callBusinessServiceByName(java.lang.String, com.kathez.entity.KathezEntity)
	 */
	@Override
	public Object callBusinessServiceByName(String serviceName,
			BusinessOperable entityObject) {
		Object returnObject = null;
		if(serviceName != null && entityObject != null){
			switch(ServiceName.valueOf(serviceName)){
				case ENROLL_STUDENT:
					returnObject = this.enrollStudent((Student)entityObject);
					break;
				case UPDATE_ENROLLMENT:
					returnObject = this.updateEnrollmentDetails((Student)entityObject);
					break;
				case DELETE_ENROLLMENT:
					returnObject = this.deleteEnrollment((Student)entityObject);
					break;
				case FETCH_ENROLLMENT:
					returnObject = this.fetchEnrollmentDetails((Student)entityObject);
					break;
				case VALIDATE_ENROOMENT:
					returnObject = this.validataStudent((Student)entityObject);
					break;
			}
		}
		return returnObject;
	}

	/* (non-Javadoc)
	 * @see com.kathez.business.IKathezDelegatableBusiness#getNamedBusinessServices()
	 */
	@Override
	public List<String> getNamedBusinessServices() {
		if(this.namedBusinessServices == null){
			this.namedBusinessServices = new ArrayList<String>();
			for(ServiceName service : ServiceName.values()){
				this.namedBusinessServices.add(service.getServiceName());
			}			
		}
		return this.namedBusinessServices;
	}
	
	private IKathezDao daoObject = null;
	private List<String> namedBusinessServices =null;
	
	private IKathezDao getDAOObject(){
		if(this.daoObject == null)
			this.daoObject = KathezObjectFactory.getDAOObject(this.getClass().getCanonicalName());
		return this.daoObject;
	}
	
	private enum ServiceName{
		ENROLL_STUDENT ("ENROLL_STUDENT"),
		UPDATE_ENROLLMENT ("updateEnrollmentDetails"),
		DELETE_ENROLLMENT ("deleteEnrollment"),
		FETCH_ENROLLMENT ("fetchEnrollmentDetails"),
		VALIDATE_ENROOMENT ("validateEnrollmentDetails");
		
		private final String serviceName;
		ServiceName(String serName){
			this.serviceName = serName;
		}
		private String getServiceName(){
			return this.serviceName;
		}
	}

	/* (non-Javadoc)
	 * @see com.kathez.studentportal.business.Enrollment#deleteEnrollment(com.kathez.studentportal.entity.Student)
	 */
	@Override
	public Student deleteEnrollment(Student student) {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.kathez.studentportal.business.Enrollment#enrollStudent(com.kathez.studentportal.entity.Student)
	 */
	@Override
	public Student enrollStudent(Student student) {
		DAOInsertResult result = (DAOInsertResult)getDAOObject().doDataOperation(student, CRUDOperataionType.ADD);
		if(result != null){
			if(result.isInsertSuccess()){
				return (Student)result.getInsertedObject();
			}else{
				LOGGER.log(Level.WARNING, "Insert operation is not success due to "+result.getInsertErrCode()+" : "+result.getInsertErrMsg());
				return (Student)result.getInsertedObject();
			}
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.kathez.studentportal.business.Enrollment#fetchEnrollmentDetails(com.kathez.studentportal.entity.Student)
	 */
	@Override
	public List<Student> fetchEnrollmentDetails(Student criteria) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.kathez.studentportal.business.Enrollment#updateEnrollmentDetails(com.kathez.studentportal.entity.Student)
	 */
	@Override
	public Student updateEnrollmentDetails(Student student) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.kathez.studentportal.business.Enrollment#validataStudent(com.kathez.studentportal.entity.Student)
	 */
	@Override
	public Student validataStudent(Student student) {
		// TODO Auto-generated method stub
		return null;
	}

}
